package com.etooling.framework.controller;

import com.etooling.framework.entity.DataDir;
import com.etooling.framework.jpa.JpaQueryBuilder;
import com.etooling.framework.jpa.Sorts;
import com.etooling.framework.jpa.Specifications;
import com.etooling.framework.repository.DataDirRepository;
import com.etooling.framework.svo.ReplyObj;
import com.etooling.framework.utils.VoToPoUtil;
import com.etooling.framework.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping(value = "/admin/dataDir")
@Controller
@EnableAutoConfiguration
public class DataDirController {
    @Autowired
    DataDirRepository dataDirRepository;


    @RequestMapping("pageQuery")
    @ResponseBody
    ReplyObj pageQuery(HttpServletRequest request,
                       @RequestParam(defaultValue = "15", required = false, value = "pageSize") int pageSize,
                       @RequestParam(defaultValue = "1", required = false, value = "page") int page,
                       @RequestParam(defaultValue = "", required = false, value = "searchValue") String searchValue,
                       @RequestParam(defaultValue = "", required = false, value = "status") String status
    ) {

        JpaQueryBuilder pb = Specifications.<DataDir>and();
        if (WebUtils.checkStr(status)) {
            pb.eq("status", status);
        }
        if (WebUtils.checkStr(searchValue)) {
            pb.like("groupName", "%" + searchValue + "%");
        }
        String companyId = (String) WebUtils.getSessionValue(request, WebUtils.LOGIN_COMPANY_ID_KEY);
        pb.eq("companyId", companyId);
        Sort sort = Sorts.builder().asc("groupName").asc("seq").build();
        Pageable pageable = PageRequest.of(page - 1, pageSize, sort);
        return WebUtils.flushSuccess(dataDirRepository.findAll(pb.build(), pageable));
    }

    @RequestMapping("getConfByGroupName")
    @ResponseBody
    ReplyObj getConfByGroupName(HttpServletRequest request,
                                @RequestParam(defaultValue = "", required = false, value = "groupName") String groupName
    ) {
        String companyId = WebUtils.getSessionValue(request, WebUtils.LOGIN_COMPANY_ID_KEY);
        JpaQueryBuilder<DataDir> pb = Specifications.<DataDir>and();
        pb.eq("companyId", companyId);
        pb.eq("status", "启用");
        pb.eq("groupName", groupName);
        Sort sort = Sorts.builder().asc("seq").build();
        List<DataDir> s = dataDirRepository.findAll(pb.build(), sort);
        return WebUtils.flushSuccess(s);
    }


    @RequestMapping("add")
    @ResponseBody
    ReplyObj add(HttpServletRequest request, DataDir vo
    ) {
        String companyId = (String) WebUtils.getSessionValue(request, WebUtils.LOGIN_COMPANY_ID_KEY);
        vo.setCompanyId(companyId);
        dataDirRepository.save(vo);
        return WebUtils.flushSuccess();
    }


    @RequestMapping("update")
    @ResponseBody
    ReplyObj update(HttpServletRequest request, DataDir vo
    ) {
        try {
            //vo界面传入的对象，po是数据的对象，有很多时候有些属性页面传过来的东西是不全，或不能修改的，如修改时间，这时就是通过第三个属性，忽略一些属性
            DataDir po = dataDirRepository.getOne(vo.getId());
            po = (DataDir) VoToPoUtil.copyProperties(po, vo, new String[]{"companyId"});
            if (!WebUtils.checkStr(po.getCompanyId())) {
                po.setCompanyId((String) WebUtils.getSessionValue(request, WebUtils.LOGIN_COMPANY_ID_KEY));
            }
            dataDirRepository.save(po);
            return WebUtils.flushSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return WebUtils.flushFailure();
        }
    }

    @RequestMapping("del")
    @ResponseBody
    ReplyObj del(HttpServletRequest request, @RequestParam(defaultValue = "", required = false, value = "id") String id
    ) {
        try {
            dataDirRepository.deleteById(id);
            return WebUtils.flushSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return WebUtils.flushFailure();
        }
    }


}
